﻿#include "EvilCentipede.h"
#include "../../../Shared/Data/Stat.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/ServerPackets.h"
#include "../MapObject.h"
#include "../DelayedAction.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    bool EvilCentipede::getCanAttack() const
    {
        return Visible && MonsterObject::getCanAttack();
    }

    bool EvilCentipede::getCanMove() const
    {
        return false;
    }

    bool EvilCentipede::getBlocking() const
    {
        return Visible && MonsterObject::getBlocking();
    }

    EvilCentipede::EvilCentipede(MonsterInfo *info) : MonsterObject(info)
    {
        Visible = false;
    }

    void EvilCentipede::ProcessAI()
    {
        if (!Visible)
        {
            SetHP(Stats[Stat::HP]);
        }
        if (!Dead && getEnvir()->getTime() > VisibleTime)
        {
            VisibleTime = getEnvir()->getTime() + 2000;

            bool visible = FindNearby(Visible ? 7 : 3);

            if (!Visible && visible)
            {
                Visible = true;
                CellTime = getEnvir()->getTime() + 500;
                Broadcast(GetInfo());
                S::ObjectShow *tempVar = new S::ObjectShow();
                tempVar->ObjectID = ObjectID;
                Broadcast(tempVar);
                ActionTime = getEnvir()->getTime() + 2000;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            }

            if (Visible && !visible)
            {
                Visible = false;
                VisibleTime = getEnvir()->getTime() + 3000;

                S::ObjectHide *tempVar2 = new S::ObjectHide();
                tempVar2->ObjectID = ObjectID;
                Broadcast(tempVar2);

                SetHP(Stats[Stat::HP]);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
            }
        }

        MonsterObject::ProcessAI();
    }

    void EvilCentipede::Turn(MirDirection dir)
    {
    }

    bool EvilCentipede::Walk(MirDirection dir)
    {
        return false;
    }

    bool EvilCentipede::IsAttackTarget(MonsterObject *attacker)
    {
        return Visible && MonsterObject::IsAttackTarget(attacker);
    }

    bool EvilCentipede::IsAttackTarget(PlayerObject *attacker)
    {
        return Visible && MonsterObject::IsAttackTarget(attacker);
    }

    void EvilCentipede::ProcessRoam()
    {
    }

    void EvilCentipede::ProcessSearch()
    {
        if (Visible)
        {
            MonsterObject::ProcessSearch();
        }
    }

    void EvilCentipede::CompleteAttack(std::vector<std::any> &data)
    {
        std::vector<MapObject*> targets = FindAllTargets(7, getCurrentLocation(), false);
        if (targets.empty())
        {
            return;
        }

        for (int i = 0; i < targets.size(); i++)
        {
            setTarget(targets[i]);
            Attack();
        }
    }

    void EvilCentipede::ProcessTarget()
    {
        if (!getCanAttack())
        {
            return;
        }
        if (!FindNearby(7))
        {
            return;
        }

        ShockTime = 0;

        S::ObjectAttack *tempVar = new S::ObjectAttack();
        tempVar->ObjectID = ObjectID;
        tempVar->Direction = getDirection();
        tempVar->Location = getCurrentLocation();
        Broadcast(tempVar);
        DelayedAction tempVar2(DelayedType::Damage, getEnvir()->getTime() + 500);
        ActionList.push_back(&tempVar2);

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }

    void EvilCentipede::Attack()
    {
        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
        if (damage == 0)
        {
            return;
        }

        if (getTarget()->Attacked(this, damage, DefenceType::MAC) <= 0)
        {
            return;
        }

        PoisonTarget(getTarget(), 5, 15, PoisonType::Green, 2000);
        PoisonTarget(getTarget(), 15, 5, PoisonType::Paralysis, 2000);
    }

    Packet *EvilCentipede::GetInfo()
    {
        return !Visible ? nullptr : MonsterObject::GetInfo();
    }
}
